var parse=require('url').parse;
module.exports=function route(config) {

    return function (req, res, next) {
        var routers=config[req.method];
        if(!routers)return next();
        var url=parse(req.url);
        var patterns=Object.keys(routers);

        for(var pattern in patterns){
            var paramsNames=pattern.match(/:(\w+)/g);
            var fn=patterns[pattern];
            var reg,match;
            pattern=pattern.replace(/\//,'\\/').replace(/:\w+/g,'([^\\/])');
            reg=new RegExp('^'+pattern+'$');
            match=url.pathname.match(reg);
            if(match){
                if(paramsNames){
                    req.params={};
                    paramsNames.forEach(function (v, i) {
                        req.params[v.slice(1)]=match[i];
                    });
                }
                fn.call(null,req,res);
            }
        }
        next();
    }
}